$(function(){$.widget("ui.dialog",$.ui.dialog,{options:{clickOutside:false,clickOutsideTrigger:""},open:function(){var e=$(this.options.clickOutsideTrigger);var t=this;if(this.options.clickOutside){$(document).on("click.ui.dialogClickOutside"+t.eventNamespace,function(n){if($(n.target).css("display")!="none"&&$(n.target).attr("from")!='simpledialog'&&$(n.target).closest($(e)).length==0&&$(n.target).closest($(t.uiDialog)).length==0){t.close()}})}this._super()},close:function(){var e=this;$(document).off("click.ui.dialogClickOutside"+e.eventNamespace);this._super()}})})
function numpad(el,position,callback,args){
	var nump = createNumPad();
	var dialog = popupPositionNumpad(nump,el,position);
	document.tmpValueForNump = "";
	document.tmpCallbackForNump = callback;
	document.tmpArgsForNumpad = args;
	clickNumpad(dialog,el);
	el.select();
	$(el).keypress(function(e){
		if(e.keyCode == 13)
			$(".ui-dialog[dialogtype=numpad]").find("#BlankDialog").dialog("close");
	});

	function createNumPad(){
		var nump = $("#numpad");
		if(nump.length==0){
			nump = $("<div><table class='numpad'>"+
					"<tr><td val='1'>1</td><td val='2'>2</td><td val='3'>3</td></tr>"+
					"<tr><td val='4'>4</td><td val='5'>5</td><td val='6'>6</td></tr>"+
					"<tr><td val='7'>7</td><td val='8'>8</td><td val='9'>9</td></tr>"+
					"<tr><td val='OK'>OK</td><td val='0'>0</td><td val='Del'><img src='/images/NumpadDel.png' val='Del'/></td></tr>"+
				"</table></div>");
		}
		return nump;
	}
	function popupPositionNumpad(nump,el,position){
		if(position == null || position == ""){
			if(($(el).offset().top - $(document).scrollTop()) > ($(window).height()/2))
				position = "bottom";
			else
				position = "top";
		}
	
		var dialog = popupNumpad(nump,el);
		var left = 0;
		var top = 0;
		var pointer = "";
		var d = dialog.parents(".ui-dialog:first");
		if(position == "bottom"){
			if(el!=null){
				d.css("top",$(el).offset().top - d.height() - 16);
				d.css("left",$(el).offset().left + ($(el).width()/2) - (d.width()/2) + 10);
			}
			pointer = $("<div class='numpadFromBottom'><div>");
			left = (d.width()/2) - 10;
			top = d.height();
		}
		else if(position == "top"){
			if(el!=null){
				d.css("top",$(el).offset().top + $(el).height() + 16);
				d.css("left",$(el).offset().left + ($(el).width()/2) - (d.width()/2) + 10);
			}
			pointer = $("<div class='numpadFromTop'><div>");
			left = (d.width()/2) - 10;
			top = -16;
		}
		
		if(pointer != ""){
			d.css("overflow","visible");
			dialog.css("overflow","visible");
			dialog.append(pointer);
			pointer.css("left",left);
			pointer.css("top",top);
		}
		return dialog;
	}
	function popupNumpad(numpad,caller){
		var msgDialog = $("#BlankDialog").clone();
		msgDialog.dialog({
			resizable : false,
			modal: false,
			clickOutside: true,
			clickOutsideTrigger: caller,
			close: function() {
				var num = document.tmpValueForNump;
				var callback = document.tmpCallbackForNump;
				var args = document.tmpArgsForNumpad;
				delete document.tmpValueForNump;
				delete document.tmpCallbackForNump;
				delete document.tmpArgsForNumpad;
				msgDialog.dialog('destroy').remove();
				if(callback!=null) callback(caller,parseInt(num),args);
			},
		});
		var maxZ = MaxDivZIndex();
		msgDialog.parent().find('.ui-dialog-titlebar').css("display","none");
		msgDialog.parent().css("z-index",maxZ+2);
		msgDialog.parent().next().css("z-index",maxZ+1);
		
		msgDialog.css("padding",0);
		msgDialog.css("margin",0);
		msgDialog.html(numpad);
		msgDialog.css("min-height","");
		msgDialog.parents(".ui-dialog:first").css("width","auto");
		msgDialog.parents(".ui-dialog:first").attr("dialogtype","numpad");
		return msgDialog;
	}
	function clickNumpad(dialog,el){
		document.elForNump = el;
		if(dialog==null) dialog = $(".ui-dialog[dialogtype=numpad]").find("#BlankDialog");
		dialog.find("td").click(function(e){
			if(document.tmpValueForNump==null) document.tmpValueForNump="";
			if($(e.srcElement).attr("val") == "Del"){
				if(document.tmpValueForNump.length > 0) document.tmpValueForNump = document.tmpValueForNump.substr(0,document.tmpValueForNump.length-1);
			}
			else if($(e.srcElement).attr("val") == "OK"){
				$(e.srcElement).parents("#BlankDialog").dialog("close");
				return;
			}
			else{
				if(document.tmpValueForNump == "0")
					document.tmpValueForNump =  "";
				document.tmpValueForNump +=  $(e.srcElement).attr("val");
			}
			var el = document.elForNump;
			delete document.vForNump;
			if(document.tmpValueForNump == "") document.tmpValueForNump = "0";
			$(el).val(document.tmpValueForNump);
			document.setClickNumpadFontColor = $(e.srcElement).css("color");
			$(e.srcElement).css("background","#9A9A9A");
			$(e.srcElement).css("color","#fff");
			setTimeout(function(){
				$(e.srcElement).css("background","");
				$(e.srcElement).css("color",document.setClickNumpadFontColor);
				delete document.setClickNumpadFontColor;
			},150);
		});
	}
}